<template>
  <a-modal
    v-model:visible="visible"
    title-align="start"
    width="800px"
    :title="formModel.processGroupId? '详细': '新增'"
    :mask-closable=false
    :ok-loading=true
    :draggable=true
  >
    <a-scrollbar style="max-height:600px; overflow: auto;">
      <a-form
        id="addForm"
        ref="formModelRef"
        label-align="left"
        layout="vertical"
        :label-col-props=labelColProps
        :wrapper-col-props=wrapperColProps
        :model="formModel"
        :rules="rules"
      >
        <a-row justify="space-between">
          <a-col :span="11">
            <a-form-item field="processGroupName" label="流程组名称">
              <a-input
                v-model="formModel.processGroupName"
                placeholder="请输入流程组名称"
                show-word-limit
                allow-clear
                :max-length="20"
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="sort" label="显示排序">
              <a-input-number
                v-model="formModel.sort"
                mode="button"
                :default-value="0"
                :min="0"
                :max="999"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-scrollbar>
    <template #footer>
      <a-row justify="end">
        <a-button-group>
          <a-button :disabled="loading" @click="cancel">
            取消
          </a-button>
          <a-button
            type="primary"
            :loading="loading"
            :disabled="hasUpdatePermission"
            @click="submit"
          >
            确定
          </a-button>
        </a-button-group>
      </a-row>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
import { ref, defineEmits, defineExpose, computed } from 'vue';
import { addProcessGroup, updateProcessGroup, detailProcessGroup, FlowProcessGroup } from '@/api/flow/process-group';
import { permission } from '@/views/form/design/config';
import { labelColProps, wrapperColProps } from '@/components/dialog-add/config';
import { Message, ValidatedError } from '@arco-design/web-vue';
import { hasPermission } from '@/utils/permission';
import useLoading from '@/hooks/loading';

const emits = defineEmits(['submit']);

const { loading, setLoading } = useLoading(false);

const rules = {
  processGroupName: [
    { required: true, message: '请输入流程组名称' }
  ],
  sort: [
    { required: true, message: '请输入显示排序' }
  ]
};

const formModelRef = ref();

const visible = ref<boolean>(false);

const formDisable = ref<boolean>(false);

const formModel = ref<FlowProcessGroup>({});

const hasUpdatePermission = computed(() => {
  return formModel.value.processGroupId && !hasPermission([permission.PERMISSION_UPDATE]);
});

const submit = async () => {
  formModelRef.value.validate((errors: undefined | Record<string, ValidatedError>) => {
    if (errors) {
      return;
    }
    setLoading(true);
    if (formModel.value.processGroupId) {
      updateProcessGroup(formModel.value).then(() => {
        submitSuccess();
      }).finally(() => {
        setLoading(false);
      });
      return;
    }
    addProcessGroup(formModel.value).then(() => {
      submitSuccess();
    }).finally(() => {
      setLoading(false);
    });
  });
};

const submitSuccess = () => {
  cancel();
  emits('submit');
  Message.success('操作成功');
};

const open = async (record: FlowProcessGroup, disable: boolean) => {
  visible.value = true;
  if (record && record.processGroupId) {
    const { data } = await detailProcessGroup(record.processGroupId);
    formModel.value = data;
  } else {
    formModel.value = { sort: 0 };
  }
  formDisable.value = disable;
  formModelRef.value.clearValidate();
  setLoading(false);
};

const cancel = () => {
  visible.value = false;
};

defineExpose({
  open
});
</script>

<script lang="ts">
export default {
  name: 'ProcessGroupDialogAdd'
};
</script>

<style scoped lang="less">

</style>